package com.inossem.warehousemanage_tv.fragment;

import android.app.Dialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.warehousemanage_tv.R;
import com.inossem.warehousemanage_tv.adapter.BatchAdapter;
import com.inossem.warehousemanage_tv.adapter.MatsAdapter;
import com.inossem.warehousemanage_tv.bean.BaseBean;
import com.inossem.warehousemanage_tv.bean.MatsBean;
import com.inossem.warehousemanage_tv.utils.Api;
import com.inossem.warehousemanage_tv.utils.Utils;
import com.inossem.warehousemanage_tv.utils.requestUtils.InossemCallback;
import com.inossem.warehousemanage_tv.utils.requestUtils.RetrofitUtils;

import retrofit2.Response;

/**
 * 仓库概览-物料列表
 *
 * @author 詹建宇
 * @time on 2019/2/14 10:21
 * @email jianyu.zhan@inossem.com
 */
public class WareMatsFragment extends BaseFragment {

    private ListView matLv, batchLv;

    private ImageView logoImg;
    private TextView matCodeTv, matNameTv, matNumTv;

    private MatsAdapter matsAdapter;

    @Override
    public int initLayout() {
        return R.layout.fragment_ware_mats;
    }

    @Override
    public void initView() {
        matLv = mView.findViewById(R.id.matLv);
        batchLv = mView.findViewById(R.id.batchLv);

        View headerView = LayoutInflater.from(mActivity).inflate(R.layout.view_ware_mats_batch_header, null);
        batchLv.addHeaderView(headerView);

        logoImg = headerView.findViewById(R.id.logoImg);
        matCodeTv = headerView.findViewById(R.id.matCodeTv);
        matNameTv = headerView.findViewById(R.id.matNameTv);
        matNumTv = headerView.findViewById(R.id.matNumTv);
    }

    @Override
    public void initData() {
        if (mActivity.isTemporary) {
            RetrofitUtils.getRetrofit(mActivity, "詹建宇", "刘宇", "仓库概览-物料列表", "获取临时存储区物料列表")
                    .create(Api.class)
                    .getLSMatList("2")
                    .enqueue(new InossemCallback<BaseBean<MatsBean>>() {
                        @Override
                        public void success(Response<BaseBean<MatsBean>> response) {
                            MatsBean bean = response.body().getBody();
                            if (null != bean.getMaterial_list() && !bean.getMaterial_list().isEmpty()) {
                                matsAdapter = new MatsAdapter(mActivity, bean.getMaterial_list());
                                matLv.setAdapter(matsAdapter);
                                matsAdapter.setListener((position, matBean) -> setData(matBean));

                                if (null != bean.getMaterial_list().get(0).getBatch_list() && !bean.getMaterial_list().get(0).getBatch_list().isEmpty()) {
                                    setData(bean.getMaterial_list().get(0));
                                } else {
                                    setData(new MatsBean.MaterialListBean());
                                }

                            }
                        }

                        @Override
                        public void failure(Response<BaseBean<MatsBean>> response) {

                        }
                    });
        } else {
            RetrofitUtils.getRetrofit(mActivity, "詹建宇", "刘宇", "仓库概览-物料列表", "通过仓位获取物料列表")
                    .create(Api.class)
                    .getMatList(mActivity.location_id, mActivity.area_id, mActivity.position_id)
                    .enqueue(new InossemCallback<BaseBean<MatsBean>>() {
                        @Override
                        public void success(Response<BaseBean<MatsBean>> response) {
                            MatsBean bean = response.body().getBody();
                            if (null != bean.getMaterial_list() && !bean.getMaterial_list().isEmpty()) {
                                matsAdapter = new MatsAdapter(mActivity, bean.getMaterial_list());
                                matLv.setAdapter(matsAdapter);
                                matsAdapter.setListener((position, matBean) -> setData(matBean));

                                if (null != bean.getMaterial_list().get(0).getBatch_list() && !bean.getMaterial_list().get(0).getBatch_list().isEmpty()) {
                                    setData(bean.getMaterial_list().get(0));
                                } else {
                                    setData(new MatsBean.MaterialListBean());
                                }

                            }
                        }

                        @Override
                        public void failure(Response<BaseBean<MatsBean>> response) {

                        }
                    });
        }

    }

    @Override
    public void initClick() {
        logoImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showImgDialog();
            }
        });
    }

    private void setData(MatsBean.MaterialListBean matBean) {
        BatchAdapter batchAdapter = new BatchAdapter(mActivity, matBean.getBatch_list(), matBean);
        batchLv.setAdapter(batchAdapter);

        Utils.setText(matCodeTv, matBean.getMat_code());
        Utils.setText(matNameTv, matBean.getMat_name());
        Utils.setText(matNumTv, matBean.getTotal() + matBean.getName_zh());
    }

    private void showImgDialog() {
        Dialog dialog = new Dialog(mActivity, R.style.MyDialog);
        dialog.setContentView(R.layout.dialog_ware_mat_img);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }

}
